// api服务器转发
var http = require('http');
var request = require('request');
// 创建api服务器，存储数据
var server = http.createServer(function (req, res) {
  if (req.url !== '/favicon.ico') {
    res.writeHead(200, { 'Content-Type': 'text/jsonp', 'charset': 'utf8', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS' });
    var url = req.url.substring(1);
    getMsg(url, function (data) {
      res.write(data);
      res.end();
    }, function (err) {
      res.write(err);
      res.end();
    })
  }
});
server.listen(8091, function () {
  console.log('only support GET.listening... port: 8091')
})

// 获得数据
function getMsg(api, success, error) {
  request({
    url: api,
    gzip: true//api开启gzip压缩数据
  }, function (err, response, body) {
    if (err) {
      if (error) {
        error(err);
      }
    } else {
      success(body);
    }
  })
}